<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="com.gzgi.framework.*"%>
<%@ page import="com.gzgi.framework.util.*"%>
<%@ page import="com.gzgi.framework.context.*"%>
<%@ page import="com.gzgi.framework.resource.*"%>
<%@ page import="com.gzgi.workflow.jbpm.model.Product"%>
<%
     Map params = RequestUtil.getParameterMap(request);
     System.out.println(params);
     String sortname = ParamUtil.getString(params, "sidx");
	 String sortorder  = ParamUtil.getString(params, "sord");
	 if(sortname == null || sortname.equals("undefined")){
		 sortname = "productName";
	 }
	 if(sortorder == null || sortorder.equals("undefined")){
		 sortorder = "asc";
	 }
	 String orderBy = " ORDER BY "+sortname.toUpperCase()+"_  "+sortorder.toUpperCase();
	 int pageNo = ParamUtil.getInt(params, "page");
     int limit = ParamUtil.getInt(params, "rows");
	 if(limit <=0){
		 limit = 15;
	 }
	 int start = (pageNo - 1) * limit;
	 String sql = " SELECT PRODUCTID_, PRODUCTNAME_, PRODUCTDATE_, QUANTITY_, PRICE_ FROM JPX_PRODUCT " + orderBy;
     System.out.println(sql);
	 List rows = new ArrayList();
	 DataSource dataSource = null;
	 Connection con = null;
	 PreparedStatement psmt = null;
	 ResultSet rs = null;
	 int index = 0;
	 int total = 0;
	 try{
		 dataSource = (DataSource)ContextFactory.getBean("dataSource");
		 con = dataSource.getConnection();
		 psmt = con.prepareStatement(sql);
		 rs = psmt.executeQuery();
		 while(rs.next()){
			  if(start <= index && index < (start + limit)){
				 Map cellMap = new HashMap();
				 Map dataMap = new LinkedHashMap();
				 String id = rs.getString(1);
				 cellMap.put("id", id);
				 dataMap.put("productId", id);
				 dataMap.put("productName", rs.getString(2));
				 dataMap.put("productDate", DateTools.getDateTime(rs.getTimestamp(3)));
				 dataMap.put("quantity", rs.getInt(4));
				 dataMap.put("price", rs.getDouble(5));
				 cellMap.put("cell", dataMap.values().toArray());
				 rows.add(cellMap);
			   }
			  index++;
			  total++;
		 }
		 psmt.close();
		 rs.close();
		 psmt = null;
		 rs = null;
	 }catch(Exception ex){
		 ex.printStackTrace();
	 }finally{
       if(con != null){
		   con.close();
	   }
	 }

	 Map pageInfo = new HashMap();
		//当前页数设置
	pageInfo.put("page", pageNo);
		//总数据量设置
    pageInfo.put("total", new Integer((int)Math.ceil(total/limit)+1));
	pageInfo.put("records", new Integer(total));
	pageInfo.put("rows", rows);
    org.json.JSONObject object = new org.json.JSONObject(pageInfo);
    out.println( object.toString() );
    System.out.println( object.toString() );
 %>